package com.tx.admin.tagrelate.service.impl;

import com.mybatisflex.spring.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.tx.admin.tagrelate.service.TagRelateService;
import com.tx.admin.tagrelate.pojo.po.TagRelate;
import com.tx.admin.tagrelate.mapper.TagRelateMapper;
import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryWrapper;
import com.mybatisflex.core.query.QueryChain;
import com.tx.core.common.enums.CommonResponseEnum;
import com.tx.core.util.PageUtils;
import com.tx.core.util.BeanCopyUtils;
import com.tx.core.util.Utils;
import com.tx.core.common.entity.PageResult;
import com.tx.core.common.entity.SelectIdsDTO;
import java.io.Serializable;
import java.util.List;
import com.tx.admin.tagrelate.pojo.dto.TagRelateCreateDTO;
import com.tx.admin.tagrelate.pojo.dto.TagRelateUpdateDTO;
import com.tx.admin.tagrelate.pojo.dto.TagRelateListDTO;
import com.tx.admin.tagrelate.pojo.dto.TagRelateImportDTO;
import com.tx.core.common.entity.ImportExcelDTO;
import com.tx.excel.core.ExcelResult;
import java.io.OutputStream;
import jakarta.servlet.http.HttpServletResponse;
import com.tx.core.util.FileUtils;
import com.tx.excel.utils.ExcelUtils;
import lombok.SneakyThrows;
import com.tx.admin.tagrelate.pojo.vo.TagRelateVO;

/**
 * <p>
 * 标签中间表 服务实现类
 * </p>
 *
 * @author sz-admin
 * @since 2025-08-08
 */
@Service
@RequiredArgsConstructor
public class TagRelateServiceImpl extends ServiceImpl<TagRelateMapper, TagRelate> implements TagRelateService {
    @Override
    public void create(TagRelateCreateDTO dto){
        TagRelate tagRelate = BeanCopyUtils.copy(dto, TagRelate.class);
        save(tagRelate);
    }

    @Override
    public void update(TagRelateUpdateDTO dto){
        TagRelate tagRelate = BeanCopyUtils.copy(dto, TagRelate.class);
        QueryWrapper wrapper;
        // id有效性校验
        wrapper = QueryWrapper.create()
            .eq(TagRelate::getId, dto.getId());
        CommonResponseEnum.INVALID_ID.assertTrue(count(wrapper) <= 0);

        saveOrUpdate(tagRelate);
    }

    @Override
    public PageResult<TagRelateVO> page(TagRelateListDTO dto){
        Page<TagRelateVO> page = pageAs(PageUtils.getPage(dto), buildQueryWrapper(dto), TagRelateVO.class);
        return PageUtils.getPageResult(page);
    }

    @Override
    public List<TagRelateVO> list(TagRelateListDTO dto){
        return listAs(buildQueryWrapper(dto), TagRelateVO.class);
    }

    @Override
    public void remove(SelectIdsDTO dto){
        CommonResponseEnum.INVALID_ID.assertTrue(dto.getIds().isEmpty());
        removeByIds(dto.getIds());
    }

    @Override
    public TagRelateVO detail(Object id){
        TagRelate tagRelate = getById((Serializable) id);
        CommonResponseEnum.INVALID_ID.assertNull(tagRelate);
        return BeanCopyUtils.copy(tagRelate, TagRelateVO.class);
    }

    @SneakyThrows
    @Override
    public void importExcel(ImportExcelDTO dto) {
        ExcelResult<TagRelateImportDTO> excelResult = ExcelUtils.importExcel(dto.getFile().getInputStream(), TagRelateImportDTO.class, true);
        List<TagRelateImportDTO> list = excelResult.getList();
        List<String> errorList = excelResult.getErrorList();
        String analysis = excelResult.getAnalysis();
        System.out.println(" analysis : " + analysis);
        System.out.println(" isCover : " + dto.getIsCover());
    }

    @SneakyThrows
    @Override
    public void exportExcel(TagRelateListDTO dto, HttpServletResponse response) {
        List<TagRelateVO> list = list(dto);
        String fileName = "标签中间表模板";
        OutputStream os = FileUtils.getOutputStream(response, fileName + ".xlsx");
        ExcelUtils.exportExcel(list, "标签中间表", TagRelateVO.class, os);
    }

    private static QueryWrapper buildQueryWrapper(TagRelateListDTO dto) {
        QueryWrapper wrapper = QueryWrapper.create().from(TagRelate.class);
        if (Utils.isNotNull(dto.getTagId())) {
            wrapper.eq(TagRelate::getTagId, dto.getTagId());
        }
        if (Utils.isNotNull(dto.getBusId())) {
            wrapper.eq(TagRelate::getBusId, dto.getBusId());
        }
        if (Utils.isNotNull(dto.getBelongType())) {
            wrapper.eq(TagRelate::getBelongType, dto.getBelongType());
        }
        if (Utils.isNotNull(dto.getExt0())) {
            wrapper.eq(TagRelate::getExt0, dto.getExt0());
        }
        if (Utils.isNotNull(dto.getExt1())) {
            wrapper.eq(TagRelate::getExt1, dto.getExt1());
        }
        if (Utils.isNotNull(dto.getExt2())) {
            wrapper.eq(TagRelate::getExt2, dto.getExt2());
        }
        if (Utils.isNotNull(dto.getExt3())) {
            wrapper.eq(TagRelate::getExt3, dto.getExt3());
        }
        return wrapper;
    }
}